
import re


MESSAGE_PATTERN = re.compile(r'\033\[1;\d+m(?P<msg>.*?)\033\[0m')
IP_PORT_PATTERN = re.compile(r'(?P<ip>(?<![.\d])(?:\d{1,3}\.){3}\d{1,3}(?![.\d])):(?P<port>\d*)')


def __match(content, pattern):
    return re.match(pattern, content)


def __search(content, pattern):
    if not content:
        return None
    return re.search(pattern, content)


def match_int(content):
    return __match(content, r'\d+')


def search_message(content):
    match = __search(content, MESSAGE_PATTERN)
    if match:
        return match.group('msg')
    return None


def search_int(content):
    match = __search(content, r'\d+')
    if match:
        return match.group()
    return None


def search_ip(content):
    match = __search(content, r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}')
    if match:
        return match.group()
    return None


def search_port(content):
    if not content:
        return None
    if content.isdigit():
        return content
    match = __search(content, r':([0-9]+)')
    if match:
        return match.groups()[0]
    return None


def search_ip_and_port(content):
    match = __search(content, r'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):([0-9]+)')
    if match:
        return match.groups()
    return None


# if __name__ == '__main__':
#     # s = 'ss144.217.204.254:3128ss'
#     s = '1212'
#     print(search_ip(s))
#     print(search_port(s))
#     print(search_ip_and_port(s))
